Instructions

Answer the following questions and complete the exercises in RMarkdown. Please embed all of your code and push your final work to your repository. Your final lab report should be organized, clean, and run free from errors. Remember, you must remove the # for the included code chunks to run. Be sure to add your name to the author header above. For any included plots, make sure they are clearly labeled. You are free to use any plot type that you feel best communicates the results of your analysis.

Make sure to use the formatting conventions of RMarkdown to make your report neat and clean!

Load the libraries

library(tidyverse)
library(janitor)
library(ggmap)
library(leaflet)

API Key

register_stadiamaps("ece99a72-2ade-4e51-85ff-001ee7169fb5", write = FALSE)

Load the Data

For this homework, we will use the shark attack data to visualize where the attacks occurred. The data are from: State of California- Shark Incident Database.

sharks <- read_csv("data/SharkIncidents_1950_2022_220302.csv") %>% 
  clean_names() %>% 
  filter(longitude !="NA" & latitude !="NA") %>% # pulling out NA locations
  mutate(longitude = as.numeric(longitude)) # converting longitude to numeric
  1. Use the range of the latitude and longitude to build an appropriate bounding box for your map.
summary(sharks)
##  incident_num           month             day             year     
##  Length:205         Min.   : 1.000   Min.   : 1.00   Min.   :1950  
##  Class :character   1st Qu.: 6.000   1st Qu.: 7.00   1st Qu.:1984  
##  Mode  :character   Median : 8.000   Median :18.00   Median :2004  
##                     Mean   : 7.878   Mean   :16.44   Mean   :1997  
##                     3rd Qu.:10.000   3rd Qu.:25.00   3rd Qu.:2013  
##                     Max.   :12.000   Max.   :31.00   Max.   :2022  
##      time              county            location             mode          
##  Length:205         Length:205         Length:205         Length:205        
##  Class :character   Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
##                                                                             
##                                                                             
##                                                                             
##     injury             depth             species            comment         
##  Length:205         Length:205         Length:205         Length:205        
##  Class :character   Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
##                                                                             
##                                                                             
##                                                                             
##    longitude         latitude     confirmed_source   wfl_case_number   
##  Min.   :-124.7   Min.   :32.59   Length:205         Length:205        
##  1st Qu.:-123.1   1st Qu.:34.04   Class :character   Class :character  
##  Median :-122.0   Median :36.70   Mode  :character   Mode  :character  
##  Mean   :-121.4   Mean   :36.36                                        
##  3rd Qu.:-119.6   3rd Qu.:38.18                                        
##  Max.   :-117.1   Max.   :41.56
map1 <- get_stadiamap(bbox = c(left = -125, bottom = 32, right = -114, top = 42), zoom = 6, maptype = "stamen_terrain")
## ℹ © Stadia Maps © Stamen Design © OpenMapTiles © OpenStreetMap contributors.
  1. Load a map from stamen in a terrain style projection and display the map.
ggmap(map1)

  1. Build a map that overlays the recorded observations of shark attacks in California.
ggmap(map1)+
  geom_point(data = sharks, aes(x = longitude, y = latitude), color = "blue", size = 0.5, alpha = 0.5)+
  labs(title="Shark Attacks in California", x="Longitude", y="Latitude")

  1. Build a map that only shows the fatalities.
ggmap(map1)+
  geom_point(data = sharks %>% filter(injury == "fatal"), 
             aes(x = longitude, y = latitude), 
             color = "red", 
             size = 0.5, 
             alpha = 0.5)+
  labs(title="Fatal Shark Attacks in California", x="Longitude", y="Latitude")

  1. Build the same map (fatalities only), but this time make it interactive using leaflet. Also, color the points red.
leaflet(sharks %>% filter(injury == "fatal")) %>% 
  addTiles() %>% 
  addCircleMarkers(
    lng = ~longitude,
    lat = ~latitude,
    radius = 2,
    stroke = FALSE,   
    fillOpacity = 0.7,
    color = "red") %>% 

  fitBounds(lng1=-125, lat1=32, lng2=-114, lat2=42)
  1. Which county has the highest number of recorded shark attacks? Make a map that only shows the attacks in this county. (hint: it may help improve the map if you remove duplicate locations, i.e. multiple attacks at the same location.)
sharks %>% 
  group_by(county) %>% 
  summarise(count = n()) %>%
  arrange(desc(count))
## # A tibble: 21 × 2
##    county          count
##    <chr>           <int>
##  1 San Diego          24
##  2 San Mateo          19
##  3 Santa Barbara      19
##  4 Humboldt           18
##  5 Marin              16
##  6 Monterey           16
##  7 Santa Cruz         15
##  8 Sonoma             15
##  9 San Luis Obispo    14
## 10 Los Angeles         9
## # ℹ 11 more rows
ggmap(map1)+
  geom_point(data = sharks %>% filter(county == "San Diego") %>% distinct(longitude, latitude), 
             aes(x = longitude, y = latitude), 
             color = "blue", 
             size = 0.5, 
             alpha = 0.5)+
  labs(title="Shark Attacks in San Diego County", x="Longitude", y="Latitude")

Knit and Upload

Please knit your work as an .html file and upload to Canvas. Homework is due before the start of the next lab. No late work is accepted. Make sure to use the formatting conventions of RMarkdown to make your report neat and clean!